import axios from 'axios'
import React, { useEffect, useState } from 'react'
import { Table,Tag,Button, notification } from 'antd'
function Audit() {
    // 获取表格数据
    const roleObj = {
        '1': "superadmin",
        '2': "admin",
        '3': "editor"
    }
    const [dataSource, setdataSource] = useState([])
    const { roleId, region, username } = JSON.parse(localStorage.getItem("token"))

    const getAudit = async () => {
        let res = await axios.get("/news?auditState=1&_expand=category")
        console.log(res);
        const list = res.data
        setdataSource(roleObj[roleId] === 'superadmin' ? list : [
            ...list.filter(item => item.author === username),
            ...list.filter(item => item.region === region && roleObj[item.roleId] === 'editor')
        ])
    }
    const columns = [
        {
            title: '新闻标题',
            dataIndex: 'title',
            render: (title, item) => {
                return <a href={`#/news-manage/preview/${item.id}`}>{title}</a>
            }
        },
        {
            title: '作者',
            dataIndex: 'author',

        },
        {
            title: '新闻分类',
            dataIndex: 'categoryId',
        },

        {
            title: '操作',
            dataIndex: '',
            key: 'key',
            render: (item) =>
                <div>
                    <Button type='primary' onClick={()=>handleAudit(item,2,1)} >通过</Button>
                    <Button danger  onClick={()=>handleAudit(item,3,0)} >驳回</Button>
                </div>
        },
    ];
    const handleAudit=(item,auditState,publishState)=>{
        setdataSource(dataSource.filter(data=>data.id!==item.id))
        axios.patch('/news/'+item.id,{
            auditState,
            publishState
        }).then(res=>{
            notification.info({
                message:"通知",
                description:"您可以到审核管理/审核列表中查看您的新闻审核状态"
            })
        })
    }
    useEffect(() => {
        getAudit()
    }, [])
    return (
        <div>
            <Table dataSource={dataSource} columns={columns}
                rowKey={(item) => item.id}
                pagination={{
                    pageSize: 5
                }} />
        </div>
    )
}

export default Audit
